﻿using Business;
using Common;
using PayService;
using PayService.JieBao;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
//using YiCai.PayService;

namespace JieBao.PayService
{
    public class JieBaoHelper
    {
        /// 监控支付订单、如果有新的支付成功订单就加钱
        public void MonitorPay(int Level)
        {
            string username = "";
            decimal amount = 0;
            string order_no = "";
            try
            {
                TimeSpan interval = new TimeSpan(0, 0, 5);
                while (true)
                {
                    Thread.Sleep(interval);
                    if (Tool.GetStatus($"Level{Level}Switch"))
                    {
                        //充值成功，没有加钱，没有异常的订单
                        string amountScope = Tool.GetXmlValueByName("Level" + Level);
                        decimal minAmount = Convert.ToDecimal(amountScope.Split('-')[0]);
                        decimal maxAmount = Convert.ToDecimal(amountScope.Split('-')[1]);
                        List<PAY_ORDER> list_t_pay_order = new PAY_ORDER().GetALL($"amount>={minAmount} and amount<={maxAmount} and state='Y' and is_check='N' and yichang='N' and addtime>1535809204");
                        foreach (var item in list_t_pay_order)
                        {
                            username = item.username;
                            amount = Convert.ToDecimal(item.amount);
                            order_no = item.order_no;
                            string cookie = Tool.GetXmlValueByName("Cookie");                            
                            httpModel result = JBHttpHelper.PostAddMoney(username, amount, cookie, order_no);
                            
                            if (result.status == DoResult.Success)
                            {
                                item.is_check = "Y";//加钱成功 //按理说有个trade_no和trade_time
                                bool Upres = item.Update(item); 
                                if (Upres == false)
                                {
                                    LogHelper.Error($"用户【{username}】；金额【{amount}】；单号【{order_no}】；状态【加钱成功，更新订单信息失败，更新bool返回为false】；");
                                }
                                else
                                {//充值成功
                                    LogHelper.Info(result.msg);
                                }
                            }
                            else
                            { 
                                item.yichang = "Y";
                                item.yc_time = DateTime.Now.ToString();
                                item.Update(item); 
                                LogHelper.Error(result.msg);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error($"用户【{username}】；金额【{amount}】；单号【{order_no}】；状态【入款失败（{ ex.Message}）】");
            }

        }
        //远程刷新页面维持Cookie
        public void RemoteMaintainCookie()
        {
            //string PostHtmlurl = "";
            //try
            //{
            //    //1个小时
            //    TimeSpan interval = new TimeSpan(1, 0, 0);
            //    while (true)
            //    {
            //        Thread.Sleep(interval);
            //        if (Tool.GetStatus("远程刷新页面维持Cookie"))
            //        {
            //            try
            //            {
            //                string result = new YCHttpHelper().CallHtml("http://www.68320001.com/cash/man/manOnlineDeposit/queryAccountByName?accountName=cs");
            //                LogHelper.Info($" 远程刷新了：{result} \r\n");
            //            }
            //            catch (Exception ex)
            //            {
            //                LogHelper.Error($"RemoteMaintainCookie此次远程刷新错误：{ex.Message} \r\n");
            //            }
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    LogHelper.Error($"RemoteMaintainCookie远程刷新错误，线程将关闭：{ex.Message} \r\n");
            //}
            //finally
            //{
            //    GC.Collect();
            //}
        }
    }
}
